﻿using System.Collections.Generic;

namespace Raven
{
    public class GoalFollowPath : GoalComposite<RavenBot>
    {
        //a local copy of the path returned by the path planner
        private LinkedList<PathEdge> _path;

        public GoalFollowPath(RavenBot owner, LinkedList<PathEdge> path) : base(owner, GoalType.goal_follow_path)
        {
            _path = path;
        }

        public override void Activate()
        {
            _status = GoalStatus.active;
  
            //get a reference to the next edge
            PathEdge edge = _path.First.Value;

            //remove the edge from the path
            _path.RemoveFirst(); 

            //some edges specify that the bot should use a specific behavior when
            //following them. This switch statement queries the edge behavior flag and
            //adds the appropriate goals/s to the subgoal list.
            AddSubgoal(new GoalTraverseEdge(_owner, edge, _path.Count == 0));
        }

        public override GoalStatus Process()
        {
            //if status is inactive, call Activate()
            ActivateIfInactive();

            _status = ProcessSubgoals();

            //if there are no subgoals present check to see if the path still has edges.
            //remaining. If it does then call activate to grab the next edge.
            if (_status == GoalStatus.completed && _path.Count > 0)
            {
                Activate(); 
            }

            return _status;
        }

        public override void Terminate()
        {
        }
    }
}